package com.zxy.supplier_system.server.service
import com.zxy.supplier_system.server.utils.orElse404
import com.zxy.supplier_system.server.xinyuan.controller.XinyuanOrdersController
import com.zxy.supplier_system.server.xinyuan.entity.XinyuanOrder
import com.zxy.supplier_system.server.xinyuan.repository.XinyuanGoodsRepository
import com.zxy.supplier_system.server.xinyuan.repository.XinyuanOrderRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class XinyuanOrderService(
    private val orderRepository: XinyuanOrderRepository,
    private val xinyuanGoodsRepository: XinyuanGoodsRepository
) {
    @Transactional
    fun createOrder(request: XinyuanOrdersController.UpdateOrderRequest): XinyuanOrder {
        val order = XinyuanOrder().apply {
            name = request.name
            idCard = request.idCard
            phoneNumber = request.phoneNumber
            paymentFee = request.paymentFee
            governmentSubsidies = request.governmentSubsidies

            paymentDateTime = request.paymentDateTime
            address = request.address
            unionPayOrderNumber = request.unionPayOrderNumber
            invoiceNumber = request.invoiceNumber
            deliveryDateTime = request.deliveryDateTime
            serialNumber = request.serialNumber
            imei1 = request.imei1
            imei2 = request.imei2
            idCardFrontPicture = request.idCardFrontPicture
            idCardBackPicture = request.idCardBackPicture
            productImagePictures = request.productImagePictures.toMutableList()
            invoicePicture = request.invoicePicture
            unionPayInvoicePicture = request.unionPayInvoicePicture
            deliveryNotePicture = request.deliveryNotePicture
            goods = xinyuanGoodsRepository.findByIdOrNull(request.goodsId).orElse404()
        }
        return orderRepository.save(order)
    }

    fun getOrderById(id: Int): XinyuanOrder {
        return orderRepository.findByIdOrNull(id).orElse404()
    }

    @Transactional
    fun updateOrder(id: Int, request: XinyuanOrdersController.UpdateOrderRequest): XinyuanOrder {
        val order = getOrderById(id)
        request.name.let { order.name = it }
        request.idCard.let { order.idCard = it }
        request.phoneNumber.let { order.phoneNumber = it }
        request.paymentFee.let { order.paymentFee = it }
        request.governmentSubsidies.let { order.governmentSubsidies = it }

        request.paymentDateTime.let { order.paymentDateTime = it }
        request.address.let { order.address = it }
        request.unionPayOrderNumber.let { order.unionPayOrderNumber = it }
        request.invoiceNumber.let { order.invoiceNumber = it }
        request.deliveryDateTime.let { order.deliveryDateTime = it }
        request.serialNumber.let { order.serialNumber = it }
        request.imei1.let { order.imei1 = it }
        request.imei2.let { order.imei2 = it }
        request.idCardFrontPicture.let { order.idCardFrontPicture = it }
        request.idCardBackPicture.let { order.idCardBackPicture = it }
        request.productImagePictures.let { order.productImagePictures = it.toMutableList() }
        request.invoicePicture.let { order.invoicePicture = it }
        request.unionPayInvoicePicture.let { order.unionPayInvoicePicture = it }
        request.deliveryNotePicture.let { order.deliveryNotePicture = it }
        order.goods =  xinyuanGoodsRepository.findByIdOrNull(request.goodsId).orElse404()
        return orderRepository.save(order)
    }

    @Transactional
    fun deleteOrder(id: Int) {
        orderRepository.deleteById(id)
    }

    fun searchOrders(
        criteria: XinyuanOrdersController.OrderSearchCriteria,
        pageable: Pageable
    ): Page<XinyuanOrder> {
        return orderRepository.findByCriteria(criteria, pageable)
    }

}